package labox.innovation.gameserver.model.actor.instance;

import java.util.Collection;

import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.actor.L2Attackable;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.L2Trap;
import labox.innovation.gameserver.model.actor.knownlist.TrapKnownList;
import labox.innovation.gameserver.network.serverpackets.AbstractNpcInfo;
import labox.innovation.gameserver.network.serverpackets.L2GameServerPacket;
//import labox.innovation.gameserver.network.serverpackets.SocialAction;
import labox.innovation.gameserver.taskmanager.DecayTaskManager;
import labox.innovation.gameserver.templates.chars.CharTemplate;
import labox.innovation.gameserver.timer.Timer;

/**
 * 陷阱实例类
 * @author zjj
 *
 */
public class L2TrapInstance extends L2Trap {
	
	//陷阱总持续时间
	private int _totalLifeTime;
    //陷阱剩余时间
	private int _timeRemaining;

	private Timer _trapTask;

	protected L2Skill _skill;

	//是否被检测了
	protected boolean _isDetected;

	protected L2Character target;
	
	private static final int TICK = 1000; //1秒

	/**
	 * @param objectId
	 * @param template
	 * @param owner
	 */
	public L2TrapInstance(long objectId, CharTemplate template, FuzePcInstance owner, int lifeTime, L2Skill skill) {
		super(objectId, template, owner);
		if (lifeTime != 0) {
			_totalLifeTime = lifeTime;
		} else {
			_totalLifeTime = 30000;
		}
		_timeRemaining = _totalLifeTime;
		_skill = skill;

		_trapTask = new Timer(TICK, true, new TrapTask(getOwner(), this, _skill));
		_trapTask.start();

	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#doDie(labox.innovation.gameserver.model.actor.L2Character)
	 */
	@Override
	public boolean doDie(L2Character killer) {
		if (!super.doDie(killer))
			return false;

		_totalLifeTime = 0;
		DecayTaskManager.getInstance().addDecayTask(this);
		return true;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getKnownList()
	 */
	@Override
	public TrapKnownList getKnownList() {
		return (TrapKnownList) super.getKnownList();
	}

	@Override
	public void initKnownList() {
		setKnownList(new TrapKnownList(this));
	}
	
	/**
	 * 技能触发
	 * @author zjj
	 * 
	 */
	class TriggerTask implements Runnable
	{
		L2Character _target;
		
		public TriggerTask( L2Character target )
		{
			_target = target;
		}
		
		public void run()
		{
			try
			{
				activate( _target );
//				doCast(_skill);
			}
			catch (Exception e)
			{
				unSummon( null );
			}
			
		}
		
	}// end 

	static class TrapTask implements Runnable {
		private FuzePcInstance _activeChar;
		private L2TrapInstance _trap;
//		private L2Skill _skill;
		
		TrapTask(FuzePcInstance activeChar, L2TrapInstance trap, L2Skill skill) {
			_activeChar = activeChar;
			_trap = trap;
//			_skill = skill;
		}

		public void run() {
			try {
				
				_trap.decTimeRemaining( TICK );
				if( _trap.getTimeRemaining() < _trap.getTotalLifeTime() - 15000 )
				{
					
				}
				
				//陷阱时间到了 那么解除陷阱
				if( _trap.getTimeRemaining() <= 0 )
				{
					_trap.unSummon(_activeChar);
				}
				
				//获得陷阱目标
				L2Character trg = _trap.setTarget();
				
				//
				if (trg != null) {
					
					//如果陷阱 是第一次被触发， 那么像周围玩家广播 。 使玩家能看到这个陷阱
					if( !_trap.isTriggered() )
					{
						//TODO: 改成 TrapInfo
						_trap.broadcastPacketByTrigered(new AbstractNpcInfo.NpcInfo(null, null));
						_trap.triggered();
					}
					
					_trap.activate(trg);
					
				}
				
				
//					for (L2Character target : _trap.getKnownList().getKnownCharactersInRadius(_skill.getSkillRadius()))
//					{
//						if (!_trap.checkTarget(target))
//							continue;
//
//						_trap.activate(target);
//						return;
//					}
				
				
				
//				//待删除， 下面的代码
//				//TODO:
//				double newTimeRemaining;
//				_trap.decTimeRemaining(1000);
//				newTimeRemaining = _trap.getTimeRemaining();
//				if (newTimeRemaining < _trap.getTotalLifeTime() - 15000) {
////					SocialAction sa = new SocialAction(_trap.getObjectId(), 2);
////					_trap.broadcastPacket(sa);
//				}
//				if (newTimeRemaining < 0) {
//					L2Character trg = _trap.setTarget();
//					if (trg != null) {
//						_trap.activate(trg);
//					} else {
//						_trap.unSummon(_activeChar);
//					}
//				}
				
			} catch (Exception e) {
				_log.error("{}",e);
			}
			
		}
	}

	/**
	 * Returns the Target of this Trap
	 * @return
	 */
	protected L2Character setTarget() {
		for (L2Character trg : this.getKnownList().getKnownCharactersInRadius(_skill.getSkillRadius())) {
			if (trg == getOwner())
				continue;

			if ((getOwner().getParty() != null && trg.getParty() != null) && getOwner().getParty().getPartyLeaderOID() == trg.getParty().getPartyLeaderOID())
				continue;

			if (trg.isInsideZone(L2Character.ZONE_PEACE))
				continue;

			return trg;
		}
		return null;
	}

	/**
	 * 
	 * @param trg
	 */
	public void activate(L2Character trg) {
		setTarget(trg);
		doCast(_skill);
		//		try
		//		{
		//			wait(_skill.getHitTime() + 2000);
		//		}
		//		catch (Exception e)
		//		{
		//			
		//		}
//		unSummon(getOwner());

//		L2Object[] targetList = _skill.getTargetList(this);
//
//		if (targetList.length == 0)
//			return;
//
//		for (L2Object atked : targetList) {
//			if (atked == getOwner())
//				continue;
//
//			if (atked instanceof FuzePcInstance)
//				continue;
//
//			if (atked instanceof L2Attackable)
//				((L2Attackable) atked).addDamage(getOwner(), 1, null);
//		}
		
	}
	
	
	@Override
	public boolean canSee(L2Character cha)
	{
		if (getOwner() == null || cha == null)
			return false;

		if (cha == getOwner())
			return true;

		if (getOwner().isInParty()
				&& cha.isInParty()
				&& getOwner().getParty().getPartyLeaderOID() == cha.getParty().getPartyLeaderOID())
			return true;

		return false;
	}
	

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Trap#unSummon(labox.innovation.gameserver.model.actor.instance.FuzePcInstance)
	 */
	@Override
	public void unSummon(FuzePcInstance owner) {
		if (_trapTask != null) {
			_trapTask.cancel();
			_trapTask = null;
		}
		super.unSummon(owner);
	}

	/**
	 * 
	 * @param value
	 */
	public void decTimeRemaining(int value) {
		_timeRemaining -= value;
	}

	/**
	 * 
	 * @return
	 */
	public int getTimeRemaining() {
		return _timeRemaining;
	}

	/**
	 * 
	 * @return
	 */
	public int getTotalLifeTime() {
		return _totalLifeTime;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Trap#setDetected()
	 */
	@Override
	public void setDetected() {
		_isDetected = true;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Trap#isDetected()
	 */
	@Override
	public boolean isDetected() {
		return _isDetected;
	}
	
	/**
	 * 检查目标
	 * @param target
	 * @return
	 */
	protected boolean checkTarget(L2Character target)
	{
		return L2Skill.checkForAreaOffensiveSkills(this, target, _skill, false);
	}
	

	/**
	 * 当陷阱触发时 向能看到的玩家 广播
	 * @param mov
	 */
	public void broadcastPacketByTrigered(L2GameServerPacket mov)
	{
		Collection<FuzePcInstance> plrs = getKnownList().getKnownPlayers().values();
		for (FuzePcInstance player : plrs)
			if (player != null && (_isTriggered && !canSee(player)))
				player.sendPacket(mov);
	}

	
}
